package com.kzj.mls.request;

import com.kzj.mls.activity.GBaseActivity;
import com.kzj.mls.global.Constant;
import com.kzj.mls.recyclerCard.basic.MaterialListView;
import com.kzj.mls.recyclerCard.basic.SuperSwipeRefresh;
import com.loopj.android.http.RequestParams;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 后台接口管理
 * @author xyg
 */
public class RequestServerManager implements IRequestServer {

    private static RequestServerManager instance;
    private IRequestServer iRequestServer;

    private RequestServerManager(IRequestServer iRequestServer)
    {
        this.iRequestServer=iRequestServer;
    }
    public static RequestServerManager getInstance()
    {
        if (instance==null) {
            instance=new RequestServerManager(RequestServer.getInstance());
        }
        return instance;
    }


    
   

    //有动态参数才调用
    /**
     * 
     * @param key 
     * @param unKnowValueMap map文件数组
     * @param unKnowValueMapString map字符数组
     * @param paramsValue 普通字符串
     * @return
     */
    public RequestParams getRequestParams(String key,HashMap<String, File> unKnowValueMap,HashMap<String, String> unKnowValueMapString,String... paramsValue) {
    	//上传图片用到unKnowValueMap，动态添加进去,没有就传个nul
      
        RequestParams params = new RequestParams();
		String[] values = Constant.paramsMap.get(key);
		
		if(values != null){
			int i = 0;
			for (; i < paramsValue.length; i++) {
				params.put(values[i], paramsValue[i]);
			}
		}
		if (unKnowValueMapString!=null) {
			Iterator<?> iter = unKnowValueMapString.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				try {
					String s=entry.getValue().toString();
					params.put( entry.getKey().toString(),s);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		if (unKnowValueMap!=null) {
			Iterator<?> iter = unKnowValueMap.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
                File f=(File)entry.getValue();
                if(f!=null&& f.exists()) {
                    try {
                        //File filew[]=new File[]{f,f,f};
                        final String contentType = RequestParams.APPLICATION_OCTET_STREAM;
                        params.put(entry.getKey().toString(),f, contentType);
                        //  params.put("fileOne", f, contentType, "fileOne");
                        //  params.put("fileTwo", f, contentType,entry.getKey().toString());
                        //  params.put("uploadedFile", f, contentType, "customFileThree");
                        //   params.put("uploadedFile", f, "multipart/form-data",entry.getKey().toString());
                        //   params.put("uploadedFile",filew, contentType);

                        //File filew[]=new File[]{f,f,f};
                        //params.put( entry.getKey().toString()+"_1", f);
                        //params.put( entry.getKey().toString()+"_2", f);
                        params.setHttpEntityIsRepeatable(true);
                        params.setUseJsonStreamer(false);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
			}
		}

//		System.out.println(key+params.toString());
        return params;
    }
    /**
     * 有P 又有T的
     * @param key
     * @param paramsValue
     * @return
     */
    public RequestParams getRequestParams(String key,String... paramsValue) {
    	//上传图片用到unKnowValueMap，动态添加进去,没有就传个nul
      
        RequestParams params = new RequestParams();
		String[] values = Constant.paramsMap.get(key);
		
		if(values != null){
			//params.put("t", key);
			int i = 0;
			for (; i < paramsValue.length; i++) {
				params.put(values[i], paramsValue[i]);
			}
		}
        System.out.println("key======"+params.toString());
        return params;
    }
    /**
     * 只有P的
     * @param key
     * @param paramsValue
     * @return
     */
    public RequestParams getRequestParamsP(String key,String... paramsValue) {
    	//上传图片用到unKnowValueMap，动态添加进去,没有就传个nul
    	
    	RequestParams params = new RequestParams();
    	/*String[] values = paramsMap.get(key);
    	
    	if(values != null){
//    		params.put("t", key);
    		int i = 0;
    		for (; i < paramsValue.length; i++) {
    			params.put(values[i], paramsValue[i]);
    		}
    	}
    	*/
    	return params;
    }
 /*   public static HttpEntity GetMultipartEntity(String key,final List<Map<String, File>> filesList,String... paramsValue) {
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();

        String[] values = Constant.paramsMap.get(key);

        if(values != null){
            //params.put("t", key);
            int i = 0;
            for (; i < paramsValue.length; i++) {
                builder.addTextBody(values[i],  paramsValue[i], ContentType.TEXT_PLAIN.withCharset("UTF-8"));
            }
        }

        if (filesList !=null && filesList.size()>0) {
            for (int i = 0; i < filesList.size(); i++) {
                Map<String, File> mapFile=filesList.get(i);
                Set<Entry<String, File>> entries = mapFile.entrySet();
                for (Entry<String, File> entry : entries) {
                    builder.addPart(entry.getKey(), new FileBody(entry.getValue()));
                }
            }
        }

        return builder.build();
    }*/
/*    @Override
    public <T extends IBaseResponse> void handleMethod(String url, RequestParams params, Class<T> instanceClass, ICallBackForRequestServer<T> callBack) {
        iRequestServer.handleMethod(url, params, instanceClass, callBack);
    }*/
	/*@Override
	public <T extends IBaseResponse> void handleMethodMultipart(String url,
			HttpEntity httpentity, Class<T> instanceClass,
			ICallBackForRequestServer<T> callBack) {
		iRequestServer.handleMethodMultipart(url, httpentity, instanceClass, callBack);
		
	}*/

	@Override
	public <T extends IBaseResponse> void handleMethod(
			GBaseActivity gBaseActivity, MaterialListView materialListView,
			SuperSwipeRefresh swipeRefresh,boolean isLoadMore,
			String url, RequestParams requestParams, Class<T> instanceClass,
			ICallBackForRequestServer<T> callBack) {
		iRequestServer.handleMethod(gBaseActivity, materialListView, swipeRefresh,isLoadMore, url, requestParams, instanceClass, callBack);
	}

}
